﻿using System;
using System.Data;
using DataBase;

namespace YJKDataLookAfter
{
	public class DbUserMessage : DbObject
	{
		public DbUser Sender
		{
			get
			{
				return this.mSender;
			}
		}

		public DbUser Receiver
		{
			get
			{
				return this.mReceiver;
			}
		}

		public DateTime Date
		{
			get
			{
				return this.mDate;
			}
		}

		public string Text
		{
			get
			{
				return this.mText;
			}
		}

		public ReadStatus Status
		{
			get
			{
				return this.mStatus;
			}
			set
			{
				this.mStatus = value;
			}
		}

		public string StatusString
		{
			get
			{
				if (this.mStatus == ReadStatus.Readed)
				{
					return "已读";
				}
				return "未读";
			}
		}

		public DbUserMessage(int id, DbUser receiver, DbUser sender, DateTime time, string txt, ReadStatus status, IDatabase db)
		{
			this.mId = id;
			this.mReceiver = receiver;
			this.mSender = sender;
			this.mDate = time;
			this.mText = txt;
			this.mStatus = status;
			this.mDatabase = db;
		}

		public static DbUserMessage GetDbMessage(int id, IDatabase db)
		{
			DbUserMessage result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("select SenderUid, ReceiverUid,Message,Status,Date from tbUserMessage where Id={0}", id);
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable != null && dataTable.Rows.Count > 0)
				{
					DbUser user = DbUser.GetUser(dataTable.Rows[0]["ReceiverUid"].ToString().Trim(), db);
					DbUser user2 = DbUser.GetUser(dataTable.Rows[0]["SenderUid"].ToString().Trim(), db);
					DateTime time = Convert.ToDateTime(dataTable.Rows[0]["Date"].ToString());
					result = new DbUserMessage(id, user, user2, time, dataTable.Rows[0]["Message"].ToString(), (ReadStatus)Convert.ToInt32(dataTable.Rows[0]["Status"].ToString()), db);
				}
				else
				{
					result = null;
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public override void RemoveFromDb()
		{
			try
			{
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				string sql = string.Format("DELETE FROM tbUserMessage WHERE Id = {0}", base.Id);
				base.Database.ExecuteCommand(sql);
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				throw ex;
			}
		}

		public override void UpdateToDb()
		{
			try
			{
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				string sql = string.Format("UPDATE tbusermessage SET Status = {0} WHERE Id ={1}", (int)this.Status, base.Id);
				base.Database.ExecuteCommand(sql);
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				throw ex;
			}
		}

		public override void AddToDb(IDatabase db)
		{
			throw new NotImplementedException();
		}

		public void SetStatus(ReadStatus Status)
		{
			this.mStatus = Status;
		}

		private DbUser mSender;

		private DbUser mReceiver;

		private DateTime mDate;

		private string mText;

		private ReadStatus mStatus;
	}
}
